﻿using JinfuyuanProject.Entity;
using JinfuyuanProject.Repository.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace JinfuyuanProject.Repository
{
    public class PackageServicesRepository:IPackageServicesRepository
    {
        private readonly JinfuyuanDbContext _db;

        public PackageServicesRepository(JinfuyuanDbContext db)
        {
            _db=db; 
        }
        public bool Delete(PackageServices packageServices)
        {
            try
            {
                _db.Entry(packageServices).State = System.Data.Entity.EntityState.Deleted;
                return _db.SaveChanges() > 0;
            }
            catch
            {
                return false;
            }
        }

        public IQueryable<PackageServices> GetList()
        {
            try
            {
                return _db.PackageServices;
            }
            catch
            {
                return null;
            }
        }
     

        public PackageServices GetModel(int Id)
        {
            try
            {
                PackageServices packageServices = _db.PackageServices.Find(Id);
                return packageServices;
            }
            catch
            {
                return null;
            }
        }

        public int Insert(PackageServices packageServices)
        {
            try
            {
                _db.Entry(packageServices).State = System.Data.Entity.EntityState.Added;
                _db.SaveChanges();
                return packageServices.Id;
            }
            catch (Exception ex)
            {
                return 0;
            }
        }
    
        public bool Update(PackageServices packageServices)
        {
            try
            {
                _db.Entry(packageServices).State = System.Data.Entity.EntityState.Modified;
                return _db.SaveChanges() > 0;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        public IEnumerable<T> QueryBySql<T>(string sql)
        {
            try
            {
                return _db.Database.SqlQuery<T>(sql);
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        #region 业务方法
        public IQueryable<PackageServices> GetList(Expression<Func<PackageServices, bool>> filter = null)
        {
            try
            {
                // 如果有过滤条件，则应用过滤条件；否则返回所有数据
                IQueryable<PackageServices> query = _db.PackageServices;

                if (filter != null)
                {
                    query = query.Where(filter);
                }

                return query;
            }
            catch
            {
                return null;
            }
        }
        public int InsertBatch(List<PackageServices> packageservicesList)
        {
            if (packageservicesList != null && packageservicesList.Count > 0)
            {
                
                    _db.PackageServices.AddRange(packageservicesList);  
                    var rowsAffected = _db.SaveChanges();  

                    return rowsAffected; 
             
            }
            return 0;  
        }
        //批量删除
        public bool DeleteBatch(List<PackageServices> packageServicesList)
        {
                foreach (var packageService in packageServicesList)
                {
                    _db.Entry(packageService).State = System.Data.Entity.EntityState.Deleted;
                }
                return _db.SaveChanges() > 0;
            
            
        }
        #endregion
    }
}
